# -*- coding: utf-8 -*-
"""
@Time : 2024/2/24 9:39
@Author : ChenXiaoliang
@Email : middlegod@sina.com
@File : get_stocks.py
"""

import requests


def get_stocks():
    sh_sz = []
    for i in range(1, 11):
        url = "https://stock.xueqiu.com/v5/stock/screener/quote/list.json?page=" + str(
            i) + "&size=60&order=desc&orderby=percent&order_by=percent&market=CN&type=sh_sz"
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
                    (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
            "Cookie": "xqat=52dfb79aed5f2cdd1e7c2cfc56054ac1f5b77fc3"
        }
        try:
            datas = requests.get(url, headers=headers).json()['data']['list']
        except KeyError:
            print("可能cookie中的xqat失效，请替换")
        for data in datas:
            # 筛选出涨幅大于4.0%股
            # if data['percent'] > 4.0:
            #     sh_sz.append(data['name'])
            # 存储每个股票的名称和代码
            sh_sz.append({'name': data['name'], 'code': data['symbol']})

    return sh_sz


if __name__ == '__main__':
    r = get_stocks()
    print(len(r), r)
